<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item>课程管理</el-breadcrumb-item>
      <el-breadcrumb-item>课程列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-row :gutter="14">
        <el-col :span="6">
          <el-input @change="search" @clear="search" v-model="searchName" :clearable="true" placeholder="请输入内容"
            prefix-icon="el-icon-search"></el-input>
        </el-col>
        <el-col :span="2">
          <el-button type="primary" @click="search">搜索</el-button>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" @click="addDialogFormVisible = true">添加</el-button>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-table :data="tableData" style="width: 100%">
            <el-table-column type="index" width="100">
            </el-table-column>
            <el-table-column prop="id" label="ID" width="180">
            </el-table-column>
            <el-table-column prop="name" label="名称" width="180">
            </el-table-column>
            <el-table-column prop="price" label="价格">
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button type="primary" icon="el-icon-edit" size="small" circle @click="edit(scope.row)"></el-button>
                <el-button type="danger" icon="el-icon-delete" size="small" circle @click="del(scope.row)"></el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
            :current-page="currentPage" :page-sizes="[3, 4]" :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper" :total="total">
          </el-pagination>
        </el-col>
      </el-row>
    </el-card>
    <!-- 添加 -->
    <el-dialog title="添加课程" :visible.sync="addDialogFormVisible">
      <el-form :model="form">
        <el-form-item label="名称" label-width="80px">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="价格" label-width="80px">
          <el-input v-model="form.price" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCourse">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 修改 -->
    <el-dialog title="修改课程" :visible.sync="editDialogFormVisible">
      <el-form :model="form">
        <el-form-item label="名称" label-width="80px">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="价格" label-width="80px">
          <el-input v-model="form.price" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        tableData: [],
        //添加模态框
        addDialogFormVisible: false,
        //修改模态框
        editDialogFormVisible: false,
        //添加form
        form: {
          id: 0,
          name: '',
          price: 0
        },
        //分页
        currentPage: 1,
        pageSize: 3,
        total: 0,
        //搜索
        searchName: ''
      }
    },
    mounted() {
      this.getClist(this.currentPage, this.pageSize);
    },
    methods: {
      //保存修改
      save() {
        let courseStr = localStorage.getItem("course");
        let courseObj = JSON.parse(courseStr);
        courseObj.forEach(val => {
          if (val.id == this.form.id) {
            val.name = this.form.name;
            val.price = this.form.price;
          }
        });
        //存储
        localStorage.setItem("course", JSON.stringify(courseObj));
        //关闭模态框
        this.editDialogFormVisible = false;
        //重写加载最新数据
        this.getClist(this.currentPage, this.pageSize)
      },
      //修改
      edit(r) {
        let courseStr = localStorage.getItem("course");
        let courseObj = JSON.parse(courseStr);
        let course = courseObj.find(function(val) {
          return val.id == r.id
        });
        //给弹框中的input赋值
        this.form.id = course.id;
        this.form.name = course.name;
        this.form.price = course.price;
        //弹框
        this.editDialogFormVisible = true;

      },
      //搜索
      search() {
        /* let courseStr = localStorage.getItem("course");
        let courseObj = JSON.parse(courseStr);
        const that = this;
        courseObj = courseObj.filter(function(val) {
          return val.name.includes(that.searchName);
        }) */
        this.getClist(this.currentPage, this.pageSize, this.searchName);
      },
      //切换当前页 val当前页数
      handleCurrentChange(currentPage) {
        this.currentPage = currentPage;
        this.getClist(currentPage, this.pageSize);
      },
      //切换每页显示的条数 val条数
      handleSizeChange(pageSize) {
        this.pageSize = pageSize;
        this.getClist(this.currentPage, pageSize);
      },
      getClist(currentPage, pageSize, searchName) {
        //searchName不传 undefined,就是false, 传了,就是true
        let courseStr = localStorage.getItem("course");
        let courseObj = JSON.parse(courseStr);
        //搜索时，需要过滤
        if (searchName) {
          courseObj = courseObj.filter(function(val) {
            return val.name.includes(searchName);
          })
        }
        //当前页
        // let currentPage = 1;
        //每页显示条数
        // let pageSize = 3;
        //开始下标
        let startIndex = (currentPage - 1) * pageSize;
        //结束下标
        let endIndex = currentPage * pageSize;
        //总条数
        this.total = courseObj.length;
        //包含startIndex，不包含endIndex
        courseObj = courseObj.slice(startIndex, endIndex);
        this.tableData = courseObj;
      },
      addCourse() {
        const c = {
          id: Math.ceil(Math.random() * 10000),
          name: this.form.name,
          price: this.form.price
        }
        let courseStr = localStorage.getItem("course");
        const courseObj = JSON.parse(courseStr);
        courseObj.unshift(c);
        localStorage.setItem("course", JSON.stringify(courseObj));
        //关闭模态框
        this.addDialogFormVisible = false;
        //重写获取数据
        this.getClist(this.currentPage, this.pageSize);
      },
      del(r) {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let courseStr = localStorage.getItem("course");
          let courseObj = JSON.parse(courseStr);
          courseObj = courseObj.filter(function(val) {
            return val.id != r.id;
          });
          localStorage.setItem("course", JSON.stringify(courseObj));
          this.getClist(this.currentPage, this.pageSize);
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      }
    }
  }
</script>

<style scoped>
  .el-breadcrumb {
    margin-bottom: 20px;
  }

  .el-pagination {
    margin-top: 15px;
  }
</style>
